package com.atguigu.webadmin.controller.apartment;


import com.atguigu.common.result.Result;
import com.atguigu.model.pojo.AttrKey;
import com.atguigu.model.pojo.AttrValue;
import com.atguigu.webadmin.service.AttrKeyService;
import com.atguigu.webadmin.service.AttrValueService;
import com.atguigu.webadmin.vo.attr.AttrKeyVo;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Parameters;
import io.swagger.v3.oas.annotations.enums.ParameterIn;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;


@Tag(name = "房间属性管理")
@RestController
@RequestMapping("/admin/attr")
@CrossOrigin
public class AttrController {

    @Autowired
    private AttrKeyService attrKeyService;

    @Autowired
    private AttrValueService attrValueService;

    @Operation(summary = "新增或更新属性名称")
    @Parameters({
            @Parameter(name = "atrKey",description = "需要新增或修改的新属性",in = ParameterIn.DEFAULT)
    })
    @PostMapping("key/saveOrUpdate")
    public Result<Boolean> saveOrUpdateAttrKey(@RequestBody AttrKey attrKey) {
        //新增或更新属性名称
        boolean b = attrKeyService.saveOrUpdate(attrKey);
        return Result.ok(b);
    }

    @Operation(summary = "新增或更新属性值")
    @Parameters({
            @Parameter(name = "attrValue",description = "需要新增或更新的属性对象",in = ParameterIn.DEFAULT)
    })
    @PostMapping("value/saveOrUpdate")
    public Result<Boolean> saveOrUpdateAttrValue(@RequestBody AttrValue attrValue) {
        boolean b = attrValueService.saveOrUpdate(attrValue);
        return Result.ok(b);
    }


    @Operation(summary = "查询全部属性名称和属性值列表")
    @GetMapping("list")
    public Result<List<AttrKeyVo>> listAttrInfo() {
        List<AttrKeyVo> attrKeyVoList = attrKeyService.listAttrInfo();
        return Result.ok(attrKeyVoList);
    }

    @Operation(summary = "根据id删除属性名称")
    @Parameters({
            @Parameter(name = "attrKeyId",description = "属性名称id",in = ParameterIn.DEFAULT)
    })
    @DeleteMapping("key/deleteById")
    public Result<Boolean> removeAttrKeyById(@RequestParam Long attrKeyId) {
        //删除attrKey
        attrKeyService.removeById(attrKeyId);
        //删除attrValue
        //创建Lambda查询包装类
        LambdaQueryWrapper<AttrValue> queryWrapper = new LambdaQueryWrapper<>();
        //指定查询条件
        queryWrapper.eq(AttrValue::getAttrKeyId, attrKeyId);
        //根据查询条件即属性名称id执行逻辑删除
        boolean b = attrValueService.remove(queryWrapper);
        return Result.ok(b);
    }

    @Operation(summary = "根据id删除属性值")
    @Parameters({
            @Parameter(name = "id",description = "属性值id",in = ParameterIn.DEFAULT)
    })
    @DeleteMapping("value/deleteById")
    public Result<Boolean> removeAttrValueById(@RequestParam Long id) {
        //根据属性值id执行逻辑删除
        boolean b = attrValueService.removeById(id);
        return Result.ok(b);
    }

}
